package com.kws.merchant.service.impl;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.kws.common.config.constant.MinioCommonBucket;
import com.kws.merchant.domain.Item;
import com.kws.merchant.domain.ItemPlan;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kws.merchant.domain.ItemPlanFile;
import com.kws.merchant.domain.po.ItemPlanPo;
import com.kws.merchant.domain.po.ItemPo;
import com.kws.merchant.domain.vo.ItemPlanDetailVo;
import com.kws.merchant.domain.vo.ItemPlanStageVo;
import com.kws.merchant.mapper.ItemPlanMapper;
import com.kws.merchant.service.ItemPlanFileService;
import com.kws.merchant.service.ItemPlanService;
import com.kws.merchant.service.ItemService;
import com.kws.merchant.util.FileUtil;
import com.sun.org.apache.bcel.internal.generic.NEW;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.multipart.MultipartFile;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 报价-项目进度(ItemPlan)表服务实现类
 *
 * @author yxy
 * @since 2024-10-24 14:47:00
 */
@Service("itemPlanService")
public class ItemPlanServiceImpl extends ServiceImpl<ItemPlanMapper, ItemPlan> implements ItemPlanService {

    @Resource
    private ItemPlanMapper itemPlanMapper;
    @Autowired
    private ItemService itemService;
    @Autowired
    private ItemPlanFileService itemPlanFileService;

    @Value("${minio.ENDPOINT}")
    private String minioUrl;

    /**
     * 分页查询列表
     */
    @Override
    public Page<ItemPlan> pageList(ItemPo itemPo) {
        LambdaQueryWrapper<ItemPlan> queryWrapper = Wrappers.<ItemPlan>lambdaQuery();
        String name = itemPo.getName();//项目名称

        Page<ItemPlan> page = this.page(new Page<>(itemPo.getPageSize(), itemPo.getTotalPage()), queryWrapper);
        List<ItemPlan> records = page.getRecords();
        records.forEach(x -> {
            Item item = itemService.getById(x.getItemId());
            if (ObjectUtils.isEmpty(item)) {
                return;
            }
            x.setItemName(item.getItemName());
        });
        if (StringUtils.isNotEmpty(name)) {
//            queryWrapper.eq(ItemPlan::getName,name);
            records = records.stream().filter(x -> x.getItemName().equals(name)).collect(Collectors.toList());
        }
        page.setRecords(records);
        return page;
    }

    /**
     * 项目阶段
     *
     * @param id
     * @return
     */
    @Override
    public ArrayList<ItemPlanStageVo> itemStage(Long id) {
        ItemPlan itemPlan = this.getById(id);
        if (ObjectUtils.isEmpty(itemPlan)) {
            throw new RuntimeException("没有改项目进度");
        }
        Integer status = itemPlan.getStatus();
        Integer beforeStatus = status - 1;
        Integer afterStatus = status + 1;

        //根据项目id获取所有项目阶段数据
        List<ItemPlanFile> itemPlanFiles = itemPlanFileService.listByPlanId(id);
        ArrayList<ItemPlanStageVo> list = new ArrayList<>();
        for (ItemPlanFile x : itemPlanFiles) {
            //阶段开始为0时上阶段和当前阶段一致
            //阶段结束为4时上阶段和当前阶段一致
            ItemPlanStageVo itemPlanStageVo = new ItemPlanStageVo();
            itemPlanStageVo.setTime(x.getCreateTime());
            itemPlanStageVo.setItemPlanId(x.getItemPlanId());
            itemPlanStageVo.setStatus(x.getStatus());
            if (status == 0) {
                itemPlanStageVo.setStage("上一阶段");
                list.add(itemPlanStageVo);
                ItemPlanStageVo itemPlanStageVo1 = new ItemPlanStageVo();
                BeanUtil.copyProperties(itemPlanStageVo, itemPlanStageVo1);
                itemPlanStageVo1.setStage("当前阶段");
                list.add(itemPlanStageVo1);
                break;
            }
            if (0 < status && status < 4) {
                if (beforeStatus == x.getStatus()) {
                    itemPlanStageVo.setStage("上一阶段");
                    list.add(itemPlanStageVo);
                }
                if (status == x.getStatus()) {
                    itemPlanStageVo.setStage("当前阶段");
                    list.add(itemPlanStageVo);
                }
                if (afterStatus == x.getStatus()) {
                    itemPlanStageVo.setStage("下一阶段");
                    list.add(itemPlanStageVo);
                    break;
                }

            }
            if (status == 4) {
                if (beforeStatus == x.getStatus()) {
                    itemPlanStageVo.setStage("上一阶段");
                    list.add(itemPlanStageVo);
                }
                if (status == x.getStatus()) {
                    itemPlanStageVo.setStage("当前阶段");
                    list.add(itemPlanStageVo);
                    ItemPlanStageVo itemPlanStageVo1 = new ItemPlanStageVo();
                    BeanUtil.copyProperties(itemPlanStageVo, itemPlanStageVo1);
                    itemPlanStageVo1.setStage("下一阶段");
                    list.add(itemPlanStageVo1);
                    break;
                }
            }

        }
        return list;
    }

    /**
     * 项目进度文件上传
     *
     * @param itemPlanPo
     * @return
     */
    @Override
    public boolean upload(ItemPlanPo itemPlanPo) {
        Long itemPlanId = itemPlanPo.getItemPlanId();
        Integer status = itemPlanPo.getStatus();
        List<String> file = itemPlanPo.getFile();
        ArrayList<ItemPlanFile> itemPlanFiles = new ArrayList<>();
        file.forEach(x->{
            ItemPlanFile itemPlanFile = new ItemPlanFile();
            itemPlanFile.setItemPlanId(itemPlanId);
            itemPlanFile.setStatus(status);
            itemPlanFile.setFile(FileUtil.subFileName(x));
            itemPlanFiles.add(itemPlanFile);
        });
//        itemPlanFile.setFile(file);
        return itemPlanFileService.saveBatch(itemPlanFiles);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id
     * @return
     */
    @Override
    public List<ItemPlanDetailVo> detailById(Long id) {
        ItemPlan itemPlan = this.getById(id);
        if (ObjectUtils.isEmpty(itemPlan)){
            throw new RuntimeException("没有查询到该项目进度");
        }
        List<ItemPlanFile> itemPlanFiles = itemPlanFileService.listByPlanId(id);
        List<Integer> collect = itemPlanFiles.stream().map(x -> x.getStatus()).distinct().collect(Collectors.toList());
        ArrayList<ItemPlanDetailVo> list = new ArrayList<>();
        collect.forEach(x -> {
            ItemPlanDetailVo itemPlanDetailVo = new ItemPlanDetailVo();
            //根据项目进度id和状态查询进度文件
            List<ItemPlanFile> fileList=itemPlanFileService.listByPlanIdAndStatus(id,x);
            itemPlanDetailVo.setStatus(x);
            itemPlanDetailVo.setFiles(fileList);
            list.add(itemPlanDetailVo);
        });

        return list;
    }

    /**
     * 删除项目进度文件
     *
     * @param id
     * @return
     */
    @Override
    public Object deleteByPlanFile(Long id) {
        return itemPlanFileService.removeByIdAndStatus(id);
    }


}
